// const proxy = 'https://photo-test-community.shijue.me';
import LoginStore from '../pages/login/store.js';
import {reqHeader} from './util.js';
const proxy = 'https://500px.com.cn';

  //tribeId: '471ade40196a41ef9b5c4b6210e34cfe'
  // tribeId: 'd1ed8d6e73b94df289791049aa9f9c22',
  //   access_token: userAccountInfo.access_token,

// let tribeId = wx.getStorageSync('tribeId');
// console.log(tribeId);
function json2Form(json) {
  var str = [];
  for (var p in json) {
    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p]));
  }
  return str.join("&");
}
const getCommunityIndexdata = (paramStr, callback) => {
  wx.setStorageSync('tribeId', paramStr.tribeId);
  let userAccountInfo = LoginStore.userInfo;
  console.log(userAccountInfo);
  let postdata = {}
  postdata.tribeId = paramStr.tribeId;
  if (userAccountInfo.access_token){
    postdata.access_token = userAccountInfo.access_token;
  }
  wx.request({
    url: `${proxy}/community/tribe/tribeDetailApp`,
    data: postdata,
    header: reqHeader,
    success:(res) => {
      console.log(res);
      callback(res.data.data);
    }
  });
  console.log(postdata);
}
const getWonderfulGroupPhotos = (params,callback) => {
  console.log(params);
  let postdata = {}
  let userAccountInfo = LoginStore.userInfo;
  if (userAccountInfo.access_token) {
    postdata.access_token = userAccountInfo.access_token;
    reqHeader.access_token = userAccountInfo.access_token;
  }
  wx.request({
    url: `${proxy}/community/tribe/listTribeWonderfulGroupPhotosV2`,
    header: reqHeader,
    data:{
      // tribeId: params.tribeId,
      wxApp: true,
      ...params,
      ...postdata,
    },
    success:(res) => {
      callback(res);
    }
  });
}
const joinGroups = (params,callback) => {
  let userAccountInfo = LoginStore.userInfo;
  let postdata = {}
  // postdata.tribeId = params.tribeId;
  if (userAccountInfo.access_token) {
    postdata.access_token = userAccountInfo.access_token;
  }
  wx.request({
    url: `${proxy}/community/tribe/join`,
    data: {
      ...postdata,
      ...params,
    },
    header: reqHeader,
    success: (res) => {
      callback(res);
    }
  });
}

const communityFollow = (callback) => {
  let userAccountInfo = LoginStore.userInfo;
  let postdata = {}
  postdata.tribeId = wx.getStorageSync('tribeId');
  if (userAccountInfo.access_token) {
    reqHeader.access_token = userAccountInfo.access_token;
    postdata.access_token = userAccountInfo.access_token;
  }
  // reqHeader['content-type'] = 'application/x-www-form-urlencoded';
  console.log(postdata);
  wx.request({
    url: `${proxy}/community/tribe/v3/follow`,
    data: json2Form(postdata),
    method: 'POST',
    header: {
      'content-type':'application/x-www-form-urlencoded',
      ...reqHeader
    },
    success: (res) => {
      callback(res);
    }
  });
}
const communityUNfollow = (callback) => {
  let userAccountInfo = LoginStore.userInfo;
  let postdata = {}
  postdata.tribeId = wx.getStorageSync('tribeId');
  if (userAccountInfo.access_token) {
    reqHeader.access_token = userAccountInfo.access_token;
    postdata.access_token = userAccountInfo.access_token;
  }
  console.log(postdata);
  reqHeader['content-type'] = 'application/x-www-form-urlencoded';
  wx.request({
    url: `${proxy}/community/tribe/v3/unfollow`,
    method: 'POST', 
    data: json2Form(postdata),
    header: reqHeader,
    success: (res) => {
      callback(res);
    }
  });
}
//部落埋点函数
const communityAlilog = (arg) => {
  let userAccountInfo = LoginStore.userInfo;
  if (userAccountInfo.access_token) {
    reqHeader.access_token = userAccountInfo.access_token;
  }
  wx.request({
    url: `${proxy}/alilog/put`,
    data: { 'objId': arg},
    header: reqHeader,
    success: (res) => {
      console.log('埋点'+arg)
      console.log(res);
    }
  });
}
const _getUserInfo = (callback) => {
  let userAccountInfo = LoginStore.userInfo;
  wx.request({
    url: `${proxy}/community/v2/user/indexInfo`,
    data: {
      access_token: userAccountInfo.access_token || '',
      queriedUserId: userAccountInfo.userId,
    },
    header: reqHeader,
    success: function (res) {
      console.log(res);
      callback(res);
    }
  })
}
//https://photo-test-community.shijue.me/community/tribe/listTribeWonderfulGroupPhotosV2?tribeId=d1ed8d6e73b94df289791049aa9f9c22&page=1&size=10&//tribeWonderfulGroupId=6ca6a110fd304b7c8d5ff5894ef5e16b
module.exports = {
  proxy: proxy,
  getCommunityIndexdata: getCommunityIndexdata,
  getWonderfulGroupPhotos: getWonderfulGroupPhotos,
  joinGroups: joinGroups,
  communityFollow: communityFollow,
  communityUNfollow: communityUNfollow,
  communityAlilog: communityAlilog,
  _getUserInfo: _getUserInfo,
}